package com.ruihu.admin.module.business.log.interfacelog.domain.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ruihu.admin.common.domain.BaseEntity;
import com.ruihu.admin.common.mybatis.FastJsonTypeHandler;
import com.ruihu.admin.module.business.log.interfacelog.domain.dto.InterfaceLogAddDTO;
import com.ruihu.admin.module.business.log.interfacelog.domain.dto.InterfaceLogDTO;
import com.ruihu.admin.module.business.log.interfacelog.domain.dto.InterfaceLogUpdateDTO;
import lombok.Data;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * [ 接口日志 ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-09-16 00:33:38
 * @since JDK1.8
 */

@Data
@TableName(value = "t_interface_log", autoResultMap = true)
public class InterfaceLogEntity extends BaseEntity {

    /**
     * 主任务ID
     */
    private Long mainTaskId;

    /**
     * 子任务ID
     */
    private Long subTaskId;

    /**
     * 电芯编号
     */
    private String electricCoreCode;

    /**
     * 模块
     */
    private String module;

    /**
     * 极组编号
     */
    private String poleGroupNumber;

    /**
     * 内容
     */
    private String content;

    /**
     * 参数
     */
    @TableField(typeHandler = FastJsonTypeHandler.class, value = "param")
    private Map<String, Object> param;

    /**
     * 请求结果;请求结果 0失败 1成功
     */
    private Integer result;

    /**
     * 任务状态：0 未完成  1 已完成
     */
    private String status;

    /**
     * 失败原因
     */
    private String failReason;

    /**
     * 完成时间
     */
    private Date completionTime;

    /**
     * 乐观锁
     */
    private Integer revision;

    /**
     * 创建人
     */
    private Long createBy;


    /**
     * 更新人
     */
    private Long updateBy;


    public static InterfaceLogEntity convertToInterfaceLogEntity(InterfaceLogAddDTO addDTO) {
        if (addDTO == null) {
            return null;
        }

        InterfaceLogEntity entity = new InterfaceLogEntity();
        entity.setModule(addDTO.getModule());
        entity.setContent(addDTO.getContent());
        entity.setParam(addDTO.getParam());
        entity.setResult(addDTO.getResult());
        entity.setFailReason(addDTO.getFailReason());
        return entity;
    }


    public static InterfaceLogEntity convertToInterfaceLogEntity(InterfaceLogUpdateDTO updateDTO) {
        if (updateDTO == null) {
           return null;
        }

        InterfaceLogEntity entity = new InterfaceLogEntity();
        entity.setId(updateDTO.getId());
        entity.setModule(updateDTO.getModule());
        entity.setContent(updateDTO.getContent());
        entity.setParam(updateDTO.getParam());
        entity.setResult(updateDTO.getResult());
        entity.setFailReason(updateDTO.getFailReason());
        return entity;
    }

    public static InterfaceLogEntity convertToInterfaceLogEntity(InterfaceLogDTO dto) {
        if (dto == null) {
           return null;
        }

        InterfaceLogEntity entity = new InterfaceLogEntity();
        entity.setId(dto.getId());
        entity.setMainTaskId(dto.getMainTaskId());
        entity.setSubTaskId(dto.getSubTaskId());
        entity.setElectricCoreCode(dto.getElectricCoreCode());
        entity.setModule(dto.getModule());
        entity.setContent(dto.getContent());
        entity.setParam(dto.getParam());
        entity.setResult(dto.getResult());
        entity.setFailReason(dto.getFailReason());
        entity.setRevision(dto.getRevision());
        entity.setCreateBy(dto.getCreateBy());
        entity.setCreateTime(dto.getCreateTime());
        entity.setUpdateBy(dto.getUpdateBy());
        entity.setUpdateTime(dto.getUpdateTime());

        return entity;
    }

    public static InterfaceLogEntity copyIgnoreNullAndEmpty(InterfaceLogEntity sourceEntity, InterfaceLogEntity destEntity) {
        if (Objects.isNull(sourceEntity))
            return destEntity;
        if (Objects.nonNull(sourceEntity.getId())) {
           destEntity.setId(sourceEntity.getId());
        }
        if (StrUtil.isNotEmpty(sourceEntity.getModule())) {
           destEntity.setModule(sourceEntity.getModule());
        }
        if (StrUtil.isNotEmpty(sourceEntity.getContent())) {
           destEntity.setContent(sourceEntity.getContent());
        }
        if (Objects.nonNull(sourceEntity.getParam())) {
           destEntity.setParam(sourceEntity.getParam());
        }
        if (Objects.nonNull(sourceEntity.getResult())) {
           destEntity.setResult(sourceEntity.getResult());
        }
        if (StrUtil.isNotEmpty(sourceEntity.getFailReason())) {
           destEntity.setFailReason(sourceEntity.getFailReason());
        }
        if (Objects.nonNull(sourceEntity.getRevision())) {
           destEntity.setRevision(sourceEntity.getRevision());
        }
        if (Objects.nonNull(sourceEntity.getCreateBy())) {
           destEntity.setCreateBy(sourceEntity.getCreateBy());
        }
        if (Objects.nonNull(sourceEntity.getCreateTime())) {
           destEntity.setCreateTime(sourceEntity.getCreateTime());
        }
        if (Objects.nonNull(sourceEntity.getUpdateBy())) {
           destEntity.setUpdateBy(sourceEntity.getUpdateBy());
        }
        if (Objects.nonNull(sourceEntity.getUpdateTime())) {
           destEntity.setUpdateTime(sourceEntity.getUpdateTime());
        }

        return destEntity;
    }
}
